# -*- coding: utf-8 -*-

# Define your item pipelines here
#
# Don't forget to add your pipeline to the ITEM_PIPELINES setting
# See: https://docs.scrapy.org/en/latest/topics/item-pipeline.html
import pymysql


class MoocspiderPipeline(object):
    def __init__(self):
        self.connect = pymysql.connect(
            host='47.99.207.109',
            port=3306,
            user='root',
            password='123456',
            db='imooc',
            charset='utf8'
        )
        self.cursor = self.connect.cursor()

    def process_item(self, item, spider):
        sql = """insert into mooc_spider(name, type, `desc`, image, gurl) VALUES (%s,%s,%s,%s,%s) """
        val = (item['name'],'mongodb', item['desc'], item['image'],item['gurl'])
        self.cursor.execute(sql, val)
        self.connect.commit()
        return item
